
$(function(){
    $('#selectedPosition').bootstrapTable({
        url: '/PositionManager/selectPosition',
        method: 'get',
        showRefresh: true,  //显示刷新按钮
        search: true,
        toolbar:'#toolbar',
        striped : true,     //设置为true会有隔行变色效果
        //sidePagination:'server',//服务器端分页默认接受rows和total
        pagination:true,//客户端分页
        sidePagination:'client',
        idFiled:'position_id',
        pageNumber:1,
        pageSize:10,
        columns: [
            {
                field: 'checkId',
                checkbox:true,
                clickToSelect:true
            }, {
                field: 'position_id',
                title: '岗位id'
            },{
                field: 'name',
                title: '岗位名'
            }
            , {
                field: 'institution_name',
                title: '所属机构'
            },{
                field: 'descrip',
                title: '描述'
            },{
                field: 'salary',
                title: '工资'
            },{
                field: 'salary_id',
                visible: false
            }]
    });
});

var vm = new Vue({
    el:'#SelectPosition',
    data:{
        showList:1,
        institutionName:'',
        positionSalary:'',
        positionName:'',
        positionDes:'',
        positionName2:'',
        positionDes2:'',
        positionSalary2:'',
        institutionName2:'',
        //选课弹窗需要字段
        institutionDescrip:'',
        position:{name:'',descrip:'',institutionId:'',salaryId:'',positionId:''}
    },
    methods: {
        //***start选课的方法***
        //选课按钮
        add: function () {
            // 弹出选课对话框
            $('#addPositionDialog').modal("show");
            //回显回显下拉框
            $("#institutionName").empty();
            $("#positionSalary").empty();
            var optionName="请点击选择";
            $.ajax({
                type: "GET",
                url: "/InstitutionManager/selectInstitution",
                success: function (result) {
                    var data = result;
                    if (result != null) {
                        $("#institutionName").append("<option value=" + "prompt" + ">" + optionName + "</option>")

                        for (var i = 0; i < data.length; i++) {
                            $("#institutionName").append("<option value=" + data[i]['institution_id'] + ">" + data[i]['name'] + "</option>")
                        }

                    } else {
                        layer.alert("有问题");
                    }
                }
            });
            $.ajax({
                type:"POST",
                url:"/Salary/BSalary",
                success:function (result) {

                    $("#positionSalary").append("<option value=" + "prompt" + ">" + optionName + "</option>")
                    for(var i =0;i<result.length;i++){
                        $("#positionSalary").append("<option value="+result[i].salaryId + ">" +result[i].salary + "</option>");
                    }

                }
            });

        },
        // 选课提交按钮
        submit: function () {


            var name = $('#positionName').val();
            var desc=$('#positionDes').val();
            var institutionId = $("#institutionName option:selected").val();
            var salaryId=$("#positionSalary option:selected").val();
            if (institutionId=="prompt") {//选中了提示的option
                layer.alert('请至少选择一项机构');
                return
            }
            if (salaryId=="prompt") {//选中了提示的option
                layer.alert('请至少选择一项薪资');
                return
            }
            if (name==null||name==''||desc==null||desc==''){
                layer.alert('检查输入');
                return
            }
            vm.position.name=name;
            vm.position.descrip=desc;
            vm.position.institutionId=institutionId;
            vm.position.salaryId=salaryId;

            $.ajax({
                type: "POST",//post方法默认是表单数据提交，用requestParameter接收，如果要用jason需要配置在conmmon.js中的第一行有配置
                url: '/PositionManager/addPosition',//请求的controller
                data: JSON.stringify(vm.position),//参数
                success: function (r) {
                    if (r === true) {
                        layer.alert('添加成功');
                        //更新表格
                        vm.positionName=null;
                        vm.positionDes=null;
                        $('#selectedPosition').bootstrapTable('refresh');
                    }
                },
                error: function () {
                    layer.alert('失败');
                }
            });
        },
        del: function () {
            var rows = $("#selectedPosition").bootstrapTable('getSelections');

            if (rows.length == 0) {
                layer.alert('请选择至少一条记录');
                return;
            } else {
                layer.confirm('您确定删除吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function(index, layero){
                    var ids = new Array();
                    //遍历所有选择的行数据，取每条数据对应的ID
                    $.each(rows, function(i, row) {
                        ids[i] = row['position_id'];
                    });
                    console.log(ids);
                    $.ajax({
                        type: "POST",
                        url: "/PositionManager/delPosition",
                        data: JSON.stringify(ids),
                        success : function(r) {
                            if(r===true){
                                layer.alert('删除成功');
                                $('#selectedPosition').bootstrapTable('refresh');
                            }else{
                                layer.alert("失败");
                            }
                        },
                        error : function() {
                            layer.alert('服务器没有返回数据，可能服务器忙，请重试');
                        }
                    });
                });
            }
        },
        upd: function () {
            // 弹出选课对话框

            var rows = $("#selectedPosition").bootstrapTable('getSelections');
            if (rows.length != 1) {
                layer.alert('请选择一条记录');
                return;
            } else {
                $('#updPosition').modal("show");
                vm.institutionName2=rows[0].institution_name;
                vm.positionName2=rows[0].name;
                vm.positionDes2=rows[0].descrip;
                $("#positionSalary2").empty();
                var optionName="请点击选择";
                $.ajax({
                    type:"POST",
                    url:"/Salary/BSalary",
                    success:function (result) {

                        $("#positionSalary2").append("<option value=" + rows[0].salary_id + ">" + rows[0].salary + "</option>")
                        for(var i =0;i<result.length;i++){
                            if(result[i].salaryId!=rows[0].salary_id){
                                $("#positionSalary2").append("<option value="+result[i].salaryId + ">" +result[i].salary + "</option>");
                            }
                        }

                    }
                });
            }
        },
        submit2: function () {

            var rows = $("#selectedPosition").bootstrapTable('getSelections');
            var name = $('#positionName2').val();
            var desc=$('#positionDes2').val();

            var salaryId=$("#positionSalary2 option:selected").val();

            if (salaryId=="prompt") {//选中了提示的option
                layer.alert('请至少选择一项薪资');
                return
            }
            if (name==null||name==''||desc==null||desc==''){
                layer.alert('检查输入');
                return
            }
            vm.position.positionId=rows[0].position_id;
            vm.position.name=name;
            vm.position.descrip=desc;
            vm.position.salaryId=salaryId;

            $.ajax({
                type: "POST",//post方法默认是表单数据提交，用requestParameter接收，如果要用jason需要配置在conmmon.js中的第一行有配置
                url: '/PositionManager/updPosition',//请求的controller
                data: JSON.stringify(vm.position),//参数
                success: function (r) {
                    if (r === true) {
                        layer.alert('修改成功');
                        vm.position.positionId=null;
                        $('#selectedPosition').bootstrapTable('refresh');
                    }
                },
                error: function () {
                    layer.alert('失败');
                }
            });
        }
        //***end选课的方法***


    }
    //***end实验的方法***

});
